/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package info.vokabel;

import java.io.*;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author msonderm
 */
public class VokabelListe extends Vector{
    String file;
    
    public VokabelListe(String file){
        this.file=file;
    }
    
    public void loadSelf(){
        try {
            File f=new File(file);
            if(!f.exists()){
                f.createNewFile();
                this.add(new VokabelElement("Liste leer","Liste leer"));
                return;
            }
            BufferedReader br=new BufferedReader(new FileReader(new File(file)));
            String buf;
            if((buf=br.readLine())!=null){
                if(!buf.contains(";")){
                    this.add(new VokabelElement("Fehlerhafte Zeile","Fehlerhafte Zeile"));
                }
                this.add(new VokabelElement(buf.split(";")[0],buf.split(";")[1]));
            } else {
                this.add(new VokabelElement("Liste leer","Liste leer"));
                br.close();
                return;
            }
            while((buf=br.readLine())!=null){
                if(!buf.contains(";")){
                    this.add(new VokabelElement("Fehlerhafte Zeile","Fehlerhafte Zeile"));
                }
                this.add(new VokabelElement(buf.split(";")[0],buf.split(";")[1]));
            }
            br.close();
        } catch (Exception ex) {
            Logger.getLogger(VokabelListe.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void appendFile(String ap){
        try {
           File f=new File(file);
            if (!f.exists()) {
		f.createNewFile();
            }
            FileWriter ff=new FileWriter(file, true);
            ff.append(ap);
            ff.close();
        } catch (Exception ex) {
            Logger.getLogger(VokabelListe.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void addNewT(VokabelElement e){
        this.add(e);
        appendFile(e.getDeutsch()+";"+e.getEnglisch());
    }
}
